package com.untilov.gb.me.gui;

import java.util.Vector;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;

import com.untilov.gb.me.GoogleBookmarksMIDlet;

/**
 * Represent list of bookmarks categories.
 * 
 * @author iuntilov
 * 
 */
public class CategoriesList extends List implements CommandListener {

    private GoogleBookmarksMIDlet midlet;

    private Command backCommand;

    private Command selectCategoryCommand;

    /**
     * Constructor.
     * 
     * @param title
     * @param listType
     */
    public CategoriesList(String title, int listType,
            GoogleBookmarksMIDlet midlet) {
        super(title, listType);
        this.midlet = midlet;
        backCommand = new Command("Back", Command.BACK, 1);
        this.addCommand(backCommand);
        selectCategoryCommand = new Command("Select category", Command.ITEM, 1);
        this.setSelectCommand(selectCategoryCommand);
        this.setCommandListener(this);
        // populate list with categories
//        populateList(midlet);
    }

    public CategoriesList populateCategoriesList() {
        this.deleteAll();
        Vector list = midlet.getFeedsBuilder().getCategoriesSortedByName();
        int categoriesCount = list.size();
        for (int i = 0; i < categoriesCount; i++) {
            this.append((String) list.elementAt(i), null);
        }
        return this;
    }

    /**
     * Command processor.
     */
    public void commandAction(Command c, Displayable d) {
        if (c == backCommand) {
            midlet.showLoginForm();
        } else if (c == selectCategoryCommand) {
            midlet.showCategoryContent(this.getString(this.getSelectedIndex()));
//            midlet.getDisplay().setCurrent(
//                    midlet.getLoginFailed(" selected index: "
//                            + this.getSelectedIndex()));
        }
    }
}
